import User.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "self_login")
public class self_login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
        doSelf_login(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {

    }
    protected void doSelf_login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        User user = null;
        String email=request.getParameter("form-email");
        String passwd=request.getParameter("form-password");

        if(email.indexOf('@') != -1)
        {
            if((user = Treatment.isRight(email, passwd)) != null)
            {
                request.getSession().setAttribute("SELF", user);
                response.sendRedirect("/user.jsp");
            }
            else
            {
                System.out.println("邮箱或者密码错误！");
                response.sendRedirect("/error.jsp");
            }
        }
        else
        {
            if((user = Treatment.isRight_dgut(email, passwd)) != null)
            {
                response.sendRedirect("/user.jsp");
            }
            else
            {
                response.sendRedirect("/error.jsp");
            }
        }
    }
}
